﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunAwardFly : EnemyBase
{

    public ABullet.BulletType bulletType = ABullet.BulletType.M;
    public GameObject gunPrefab;

    public float flySpeed = 7.0f;
    public bool working = false;

    private Transform flyBody;

    private void Awake()
    {
        base.Init();
        flyBody = transform.Find("飞行物");
    }
    void Start()
    {
        health = 0;
        StopWork();
        //StartWork();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void FixedUpdate()
    {
        if (working)
        {
            rig.velocity = Vector2.right * flySpeed;
            UpdateAppear();
        }

    }

    public void StartWork()
    {
        if (!working)
        {
            animator.enabled = true;
            flyBody.gameObject.SetActive(true);
            working = true;

        }
    }
    public void StopWork()
    {
        animator.enabled = false;
        flyBody.gameObject.SetActive(false);
        working = false;
    }

    public override float Damage(GameObject source, float value)
    {
        Die();
        return 0;
    }
    public override void Die()
    {
        ThrowGun();
        Boom();
    }
    public override void Run()
    {
        StartWork();
    }

    private void Boom()
    {
        if (boomPrefab != null)
        {
            Instantiate(boomPrefab, transform.position, Quaternion.identity);
            Constant.Sounds.EnemyDeath1.PlayOneShot();
        }
        Destroy(gameObject);
    }
    private void ThrowGun()
    {
        if (gunPrefab != null)
        {
            GameObject gunObj = Instantiate(gunPrefab, transform.position, Quaternion.identity);
            if (gunObj != null)
            {
                Gun gun = gunObj.GetComponent<Gun>();
                gun.SetBulletType(bulletType);
                gun.Throw();
            }
        }
    }
    public override void Stop()
    {
        StopWork();
    }

    public override bool IsDie()
    {
        throw new System.NotImplementedException();
    }
}
